package com.niit.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DataBaseUtil {
	private static String drivername;
	private static String url;
	private static String loginname;
	private static String psword;
	static {
		try {
			ClassLoader classLoader = EmailUtil.class.getClassLoader();
			InputStream is = classLoader.getResourceAsStream("jdbc.properties");
			Properties properties = new Properties();
			properties.load(is);

			drivername = properties.getProperty("drivername");
			url = properties.getProperty("url");
			loginname = properties.getProperty("loginname");
			psword = properties.getProperty("psword");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 获取数据库连接
	 * @return Connection对象
	 */
	public static Connection getConnection(){
		Connection conn = null;
		try {
			Class.forName(drivername);
			// 获取数据库连接
			conn = DriverManager.getConnection(url,loginname,psword);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	/**
	 * 关闭数据库连接
	 */
	public static void closeConnection(Connection conn){
		// 判断conn是否为空
		if(conn != null){
			try {
				conn.close();	// 关闭数据库连接
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
